﻿/*
 * Released under Microsoft Public License (Ms-PL) 
 * Sponsored by Development Platform Evangelism unit of Microsoft Israel
 * 
 * Copyright © 2008 by Tamir Khason
 * http://blogs.microsoft.co.il/blogs/tamir/
 * http://sharpsoft.net/
 * 
 * More information including licensing and term of use
 * can be found on http://www.codeplex.com/SilverlightRTL/
 * 
 */

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;

namespace System.Windows.BidiControls
{
    public class LengthConverter : TypeConverter
    {
        private static double[] PixelUnitFactors = new double[] { 1.0, 96.0, 37.795275590551178, 1.3333333333333333 };
        private static string[] PixelUnitStrings = new string[] { "px", "in", "cm", "pt" };

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            switch (Type.GetTypeCode(sourceType))
            {
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.String:
                    return true;
            }
            return false;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object source)
        {
            if (source == null)
            {
                typeof(double).ThrowConvertFromException(source);
            }
            if (source is string)
            {
                return ConvertFromString((string)source);
            }
            return Convert.ToDouble(source);
        }        

        public new object ConvertFromString(string text)
        {
            double res = 0;
            string str = text.Trim().ToLower(CultureInfo.InvariantCulture);
            int length = str.Length;
            int px = 0;
            double rt = 1.0;
            if (str == "auto")
            {
                return double.NaN;
            }
            for (int i = 0; i < PixelUnitStrings.Length; i++)
            {
                if (str.EndsWith(PixelUnitStrings[i], StringComparison.Ordinal))
                {
                    px = PixelUnitStrings[i].Length;
                    rt = PixelUnitFactors[i];
                    break;
                }
            }
            str = str.Substring(0, length - px);
            try
            {
                res = Convert.ToDouble(str) * rt;
            }
            catch (FormatException)
            {
                typeof(double).ThrowConvertFromException(text);
            }
            return res;
        }

        public new string ConvertToString(object value)
        {
            if (value is double && double.IsNaN((double)value))
            {
                return "Auto";
            }
            return Convert.ToString(value);
        }
        
    }


}
